#include <algorithm>
#include <cstdio>
#include <cstring>

const int N = 5e2 + 5;
int n, m, t, tot[2][N];
char str[N][N];

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("B.in", "r", stdin);
  freopen("B.out", "w", stdout);
#endif
#endif

  scanf("%d", &t);
  while (t--) {
    memset(tot, 0, sizeof(tot));
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
      scanf("%s", str[i]);
      for (int j = 0; j < m; ++j) { tot[str[i][j] - '0'][j]++; }
    }
    int ans = 0;
    for (int i = 0; i < m; ++i) {
      ans = std::max(ans, std::max(2 - tot[1][i], 0));
      ans = std::max(ans, std::max(1 - tot[0][i], 0));
    }
    printf("%d\n", ans);
  }
  return 0;
}